using System;
using MvcCms.Data;
using System.Diagnostics;
using System.Reflection;
using System.Globalization;
using System.Text.RegularExpressions;
namespace MvcCms.Service.Code
{
    public class GeneralMethods
    {
        #region Constructors

        #endregion

        public static string GetAddedByAvatarUrl(DTOContentComment comment)
        {
            const int size = 16;
            string identity;
            //if authenitcated get user email
            if (comment.UserName != "anonymous")
            {
                identity = comment.Email.ToLower();
            }
            else
            {
                if (comment.AddedByAnonEmail != null)
                {
                    identity = comment.AddedByAnonEmail.Trim() != ""
                                   ? comment.AddedByAnonEmail.ToLower()
                                   : comment.AddedByIP;
                }
                else
                {
                    identity = comment.AddedByIP;
                }
            }
            return String.Format("http://www.gravatar.com/avatar/{0}?s={1}&d=identicon", identity.ToHashString("MD5"),
                                 size);
        }
        public static string GetAddedByAvatarUrl(User user)
        {
            const int size = 16;
            string identity = user.Email.ToLower();
            return String.Format("http://www.gravatar.com/avatar/{0}?s={1}&d=identicon", identity.ToHashString("MD5"),
                                 size);
        }

        public static string GetStackNames()
        {
            StackTrace stackTrace = new StackTrace();
            StackFrame stackFrame = stackTrace.GetFrame(1);
            MethodBase methodBase = stackFrame.GetMethod();
            string callingName = methodBase.Name;
            stackFrame = stackTrace.GetFrame(2);
            methodBase = stackFrame.GetMethod();
            string parentName = methodBase.Name;
            return callingName + " Called by " + parentName;
        }
        public static bool TryParseCurrency(string value, out decimal result)
        {
            result = 0;
            const int maxCount = 100;
            if (String.IsNullOrEmpty(value))
                return false;

            const string decimalNumberPattern = @"^\-?[0-9]{{1,{4}}}(\{0}[0-9]{{{2}}})*(\{0}[0-9]{{{3}}})*(\{1}[0-9]+)*$";

            NumberFormatInfo format = CultureInfo.CurrentCulture.NumberFormat;

            int secondaryGroupSize = format.CurrencyGroupSizes.Length > 1
                    ? format.CurrencyGroupSizes[1]
                    : format.CurrencyGroupSizes[0];

            var r = new Regex(String.Format(decimalNumberPattern
                                           , format.CurrencyGroupSeparator == " " ? "s" : format.CurrencyGroupSeparator
                                           , format.CurrencyDecimalSeparator
                                           , secondaryGroupSize
                                           , format.CurrencyGroupSizes[0]
                                           , maxCount), RegexOptions.Compiled | RegexOptions.CultureInvariant);
            return !r.IsMatch(value.Trim()) ? false : Decimal.TryParse(value, NumberStyles.Any, CultureInfo.CurrentCulture, out result);
        }
    }    
}